package tt.dz.biz.service.rpc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import javax.annotation.Resource;
import org.apache.avro.Protocol;
import org.apache.avro.Protocol.Message;
import org.apache.avro.generic.GenericRecord;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import tt.dz.biz.service.user.OperListService;
import tt.dz.common.DzDigester;
import tt.dz.common.DzErrorCode;
import tt.dz.common.DzErrorCode.CommonErrorCode;
import tt.dz.common.DzErrorCode.UserErrorCode;
import tt.dz.common.RPCUtil;
import tt.dz.vo.OperListVO;

@Service("rpcModifyPwdByVerifyCode")
public class RPCModifyPwdByVerifyCode
  implements RPCBaseService
{
  @Resource(name="operListService")
  private OperListService operListService;
  @Resource(name="rpcMsVerifyCodeService")
  private RPCMsVerifyCodeService rpcMsVerifyCodeService;
  
  public Object execute(Protocol.Message message, Object request, Protocol protocol)
    throws Exception
  {
    Object reqJson = ((GenericRecord)request).get("req");
    JSONObject resObj = null;
    try
    {
      JSONObject reqObj = JSON.parseObject(reqJson.toString());
      String mobileNo = reqObj.getString("mobileNo");
      String checkCode = reqObj.getString("checkCode");
      String newPassword = reqObj.getString("newPassword");
      String repeatPassword = reqObj.getString("repeatPassword");
      if ((StringUtils.isBlank(mobileNo)) || (StringUtils.isBlank(checkCode)) || (StringUtils.isBlank(newPassword)) || 
        (StringUtils.isBlank(repeatPassword)))
      {
        resObj = RPCUtil.generateResponseObject(Integer.valueOf(0), DzErrorCode.CommonErrorCode.PARAM_FORMAT_ERROR.value(), "必传参数有缺失！");
        
        return JSON.toJSONString(resObj);
      }
      if (!newPassword.equals(repeatPassword))
      {
        resObj = RPCUtil.generateResponseObject(Integer.valueOf(0), DzErrorCode.UserErrorCode.REPEAT_PWD_ERROR.value(), "请确认新密码于重复密码一致！");
        
        return JSON.toJSONString(resObj);
      }
      JSONObject checkCodeObj = this.rpcMsVerifyCodeService.verifyCheckCode(reqObj);
      String isSucess = checkCodeObj.getString("isSuccess");
      if ("1".equals(isSucess))
      {
        OperListVO operVo = this.operListService.queryOper(mobileNo);
        if (operVo != null)
        {
          operVo.setPassword(DzDigester.sha1ForOper(newPassword));
          this.operListService.updateOperList(operVo);
          resObj = RPCUtil.generateResponseObject(Integer.valueOf(1), null, null);
        }
        else
        {
          resObj = RPCUtil.generateResponseObject(Integer.valueOf(0), DzErrorCode.UserErrorCode.USER_NOT_FOUND.value(), "用户" + mobileNo + "不存在！");
        }
      }
      else
      {
        resObj = checkCodeObj;
      }
    }
    catch (JSONException e)
    {
      resObj = RPCUtil.generateResponseObject(Integer.valueOf(0), DzErrorCode.CommonErrorCode.FORMAT_ERROR.value(), DzErrorCode.CommonErrorCode.FORMAT_ERROR
        .getDesc());
    }
    catch (Exception e)
    {
      throw e;
    }
    return JSON.toJSONString(resObj);
  }
}
